﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Evan.Common;
using Evan.Common.Security;

namespace BLL
{
    public static class Utils
    {
       
        /// <summary>
        /// 请求时 token：action+guid 加密
        /// 放入缓存 action-sessionId：guid
        /// </summary>
        public static string GetTokenByAction(string action)
        {
            var guid = Guid.NewGuid().ToString();
            // var guid = CommFun.ReturnSessionID();
            var token = DESEncrypt.Encrypt(action + "," + guid);
            //放入缓存 待验证
            CacheHelp.Insert(action + "-" + CommFun.ReturnSessionID(), guid);

            return token;
        }

        public static string GetActionByToken(string token)
        {
            try
            {
                var info = DESEncrypt.Decrypt(token);
                var infoArr = info.Split(',');
                var 缓存值 = CacheHelp.GetValue(infoArr[0] + "-" + CommFun.ReturnSessionID());

                if (缓存值 != null && 缓存值.ToString() != infoArr[1])
                    return "";

                return infoArr[0];
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
        

    }
}
